/* Utility scripts for Pardus Sector Tool
 * (C) Yassen 2010
 *
 * sector.js
 * Defines functions for sectors.
 *
 * Functions:
 * Export(id)
 * Import(id, code)
 * Draw(id)
 * Clear(id)
 * IsSector(name)
 * Search(term)
 */

Global.CurrentSector = 0;
Global.SectorList = [];
Global.CurrentTile = { cell: null, x: null, y: null, id: null, tile: null };

Global.CachedSectors = $H();

Utility.Sector.Name = function(id)
{
  if(id == undefined)
    id = Global.CurrentSector;
  
  return Global.SectorList[id].name;
}

Utility.Sector.Export = function(id)
{
  if (id == undefined)
    id = Global.CurrentSector;

  return ImportModules["Current"].ExportSector(Global.SectorList[id]);
}

Utility.Sector.Import = function(code)
{
  var regex = new RegExp('"version":([0-9]*)');
  var version = regex.exec(code)[1] * 1;
  return ImportModules[version].ImportSector(code);
}

Utility.Sector.MapToTiles = function(code)
{
  var parts = code.split("|");
  var dim = parts[1].split("X");
  var width = dim[0] * 1;
  var height = dim[1] * 1;
  var data = parts[0];

  var tiles = [];
  var i = 0;

  for (var y = 0; y <= height; y++)
  {
    for (var x = 0; x <= width; x++)
    {
      tiles.push(new Tile(i, AbbrToTileType(data[i]), x, y, false));
      i++;
    }
  }

  return tiles;
}

Global.CachedSectors["Sol"] = Utility.Sector.MapToTiles("bbbeeeeeeebbbbeeeeeeeeeebeeefffffeeeeeefffoofffeeeffffffffffffffoooooffeeffffffffffffffoooooofeeeefffffffffffffoooffeeebbefffffffffffffffffeebbbbeeffffffffffffoeefebbbbbbefffffffffffffeefeeeeebbeffffffffffffffeeeeefebeefffffffffffoofebbbeeebefffggggggfffoooebbbbbbeefffgggggggfffofeeeebbbeefffgggggggggffffffeeebbefffgggggggggffffffffeebefgggfgggggggfffffffffebefggggggggggggfffffffeebefgggggfggggggggffffeebbeggggggfggggggggffffebbeeggggggggggggggfffffebbefgggggfgggggggfggggfeebefgggggfgggggggggggggfebeeggggfggggggggggggggfeebeffoffggggggggggggggffebeeooffggggggggggggggffebbeffffggggggggggggggffebeefffffggggggggggggffeeeefffeeefgggggfggggfeeebefffeebeeffeeeffffeeebbbeeeeebbbeeeebeeeeeebbbbb|23X28");

Sector.prototype.toDOM = function()
{
  if (!$chk(this.tiles))
    throw "Tiles are not defined!";

  var table = new Element("table", { "class": "map", "style": { "display": "none"} }); // Container
  table.addEvent("contextmenu", function() { return false; });
  var topRow = new Element("tr").inject(table); // Top Row
  new Element("th").inject(topRow, "top");

  var width = this.tiles[this.tiles.length - 1].x + 1;  // Last element is the extent
  var height = this.tiles[this.tiles.length - 1].y + 1;

  for (var x = 0; x < width; x++)
    new Element("th", { html: x }).inject(topRow, "bottom");

  var id = 0;

  for (var y = 0; y < height; y++)
  {
    var row = new Element("tr");
    new Element("th", { html: y }).inject(row, "bottom");

    for (var x = 0; x < width; x++)
    {
      var tile = this.tiles[id];
      var cell = new Element("td", { "class": TileTypeToAbbr(tile.type) });

      Global.CurrentTile = { cell: cell, x: x, y: y, id: id, type: tile.type };
      EventSink.fireEvent("TileCreated");
      cell = Global.CurrentTile.cell;

      cell.adopt(new Element("img", { "src": "./images/transparent.png", "height": "32px", "width": "32px" }));
      cell.inject(row, "bottom");
      id++;
    }

    row.inject(table, "bottom");
  }

  return table;
}

Utility.Sector.Draw = function(id)
{
  if (!$defined(id))
    id = Global.CurrentSector;

  var sector = $H(Global.SectorList[id]);
  Utility.Sector.Clear(id);

  // We assume that the map(s) are already made and will only draw the objects.
  // We'll use Panels.Main.Add for consistency. Bad practice, but who cares.

  for(var i = 0; i < sector.buildings.length; i++)
  {
    var building = sector.buildings[i];
    Panels.Main.Add(eBuilding, building.id, building.x, building.y, sector.tiles[building.id].type, building.level, building.name, building.owner);
  }
  for(var i = 0; i < sector.planets.length; i++)
  {
    var planet = sector.planets[i];
    Panels.Main.Add(ePlanet, planet.id, planet.x, planet.y, sector.tiles[planet.id].type, planet.population, planet.type, "");
  }
  for(var i = 0; i < sector.starbases.length; i++)
  {
    var starbase = sector.starbases[i];
    Panels.Main.Add(eStarbase, starbase.id, starbase.x, starbase.y, sector.tiles[starbase.id].type, starbase.population, starbase.owner == NPC? "npc": "player", starbase.owner);
  }
  for(var i = 0; i < sector.backgrounds.length; i++)
  {
    var background = sector.backgrounds[i];
    Panels.Main.Add(eBackground, background.id, background.x, background.y, sector.tiles[background.id].type, 0, background.image, "");
  }
}

/// Erases all map objects (not the images, though)
Utility.Sector.Clear = function(id)
{
  if(id == undefined)
    id = Global.CurrentSector;

  Global.SectorList[id].buildings = [];
  Global.SectorList[id].planets = [];
  Global.SectorList[id].starbases = [];
  Global.SectorList[id].backgrounds = [];
}

Utility.Sector.IsSector = function(name)
{
  return Global.SectorList[name] != undefined;
}

Utility.Sector.Search = function(term)
{  
  var result = [];
  term = term.toLowerCase();
  
  for(var name in Global.Sectors)
  {
    if(Global.Sectors.hasOwnProperty(name) && name.toLowerCase().search(term) != -1)
      result.push(name);
  }

  return result.sort();
}